/***************
    This code determines the sizes of the tables so that
   we can proceed to allocate memory for the histograms and
   tables. We do this by loading in a couple of tables
   and finding their dimensions


   Returns also some info on the stellar track that are stored in the
   track_info file

******************/



//Point to the data base file 

#define TAB_TIME "times_for_tables.dat"
#define TAB_MASS "mass_for_tables.dat"   
#define TAB_LOW_QHO  "lowmass_log_qh0.dat"  


using namespace std;


//allow use of global variables from table
extern long which_fluxes;
extern vector<double> tab_time_axis;
extern vector<double> tab_time_axis_indx;
extern vector<double> tab_time_cutoff;
extern vector<double> tab_mass_axis;
extern vector<double> tab_low_mass_axis;
extern vector<double> tab_low_qho;

extern float metallicity;
extern long ntables;
extern string data_dir;
extern long n_flux_matrix;
extern long n_low_matrix;
extern string libpath;

extern long nmass_axis;
void check_tables(){

//how many tables
  
  for (long bit_ct=1;bit_ct<150;bit_ct++){
     if((long) (which_fluxes & (long) pow((double) 2,(double) bit_ct)) > 0){
      ntables++;
     } 
  }

  double buffer;
  ifstream myfile;

  //get folder
  data_dir=(getenv("SLUG_DIR"));
  data_dir.append("/data/");
  
  string tabfile;
  vector<double> tab_low_qho; 
  
  data_dir.append(libpath.data());
  data_dir.append("/"); //just in case 
  
  //Now load the tables
  
  //------------------------Read 
  tabfile.assign(data_dir);
  tabfile.append(TAB_TIME);
  
  myfile.open(tabfile.data(), ios::in | ios::binary);
  if(!myfile){
    cerr << "LOAD_T: Unable to open table"<<endl;
    exit(1); 
  }
  
  // keep reading until end-of-file
  while(!myfile.eof()) { 
    myfile.read((char *)(&buffer), sizeof(buffer));
    if (!myfile.eof())tab_time_axis.push_back(buffer);
  }
  myfile.close();
  
  
  //------------------------Read 
  tabfile.assign(data_dir);
  tabfile.append(TAB_MASS);

  myfile.open(tabfile.data(), ios::in | ios::binary);
  if(!myfile){
    cerr << "LOAD_T: Unable to open table"<<endl;
    exit(1); 
  }
  
  // keep reading until end-of-file
  while(!myfile.eof()) { 
    myfile.read((char *)(&buffer), sizeof(buffer));
    if (!myfile.eof()) tab_mass_axis.push_back(buffer);
    
  }
  myfile.close();

//  n_flux_matrix=(long) tab_mass_axis.size()*tab_time_axis.size();
//ISO: this is changed because mass table now is 2D
  n_flux_matrix=(long) tab_mass_axis.size();
  nmass_axis=tab_mass_axis.size()/tab_time_axis.size();
//------------------------Read
  tabfile.assign(data_dir);
  tabfile.append(TAB_LOW_QHO);

  myfile.open(tabfile.data(), ios::in | ios::binary);
  if(!myfile){
    cerr << "LOAD_T: Unable to open table"<<endl;
    exit(1);
  }

  // keep reading until end-of-file
  while(!myfile.eof()) { 
    myfile.read((char *)(&buffer), sizeof(buffer));
    if (!myfile.eof())tab_low_qho.push_back(buffer);
  }
  myfile.close();
  n_low_matrix=(long) tab_low_qho.size();




  //now load info on the track 
  string infofile(data_dir);
  infofile.append("track_info.txt");
  FILE * inftra=fopen(infofile.data(),"r");
  if(!inftra){
    cerr << "LOAD_T: WARNING!! Unable to open track info file... set to 999"<<endl;
    metallicity=999;
  } else {
    //parse the info file
    //metal
    fscanf(inftra,"%f",&metallicity);
    fclose(inftra);
  }
  
  
  return;
}
